package com.water.controller;

import com.water.entity.PyUserhistory;
import com.water.service.PyUserhistoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

/**
 * (PyUserhistory)表控制层
 *
 * @author makejava
 * @since 2020-12-22 18:39:49
 */
@RestController
@RequestMapping("pyUserhistory")
public class PyUserhistoryController {
    /**
     * 服务对象
     */
    @Resource
    private PyUserhistoryService pyUserhistoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PyUserhistory selectOne(Integer id) {
        return this.pyUserhistoryService.queryById(id);
    }

    /**
     * 通过日期查询多条数据
     * @return 影响行数
     */
    @GetMapping("selectPyUserHistory")
    public ModelAndView selectPyUserHistory(ModelAndView model){
        List<Map<String,Object>> mapList = this.pyUserhistoryService.selectPyUserHistory();
        List<Map<String,Object>> maps = new ArrayList<>();
        if(mapList.size() > 0){
            for(int i = 0;i < mapList.size();i++){
                String str = (String)mapList.get(0).get("Datee");
                String[] arr = str.split("-");
                str = arr[0] + "年" + arr[1] + "月";
                mapList.get(i).put("date",str);
            }
            for(int i = 0;i < mapList.size();i++){
                double b = (Double) mapList.get(i).get("PayMoneys");
                double a = (Double) mapList.get(i).get("BillMoneys");
                double c = (b / a) * 100;
                String s = "" + c;
                s = s.substring(0,s.indexOf(".")+2) + "%";
                mapList.get(i).put("percent",s);
            }
            Set<Object> set = new HashSet<>();
            for(int i = 0;i < mapList.size();i++){
                String s = mapList.get(i).get("Datee").toString().substring(0,mapList.get(i).get("Datee").toString().indexOf("-")+2);
                if(!set.contains(s)){
                    set.add(s);
                }
            }
            Iterator<Object> it = set.iterator();
            List<Map<String,Object>> arrayList = new ArrayList<>();
            while (it.hasNext()){
                String[] array = it.next().toString().split("-");
                Map<String,Object> map = new HashMap<>();
                map.put("date",array[0]+"年"+array[1]+"月");
                map.put("time",array[0]+"-"+array[1]);
                arrayList.add(map);
            }
            maps.add(mapList.get(0));
            for(int i = 0;i < arrayList.size();i++){
                if(i == 0){
                    maps.get(0).put("date",arrayList.get(0).get("date"));
                    maps.get(0).put("date",arrayList.get(0).get("time"));
                }else{
                    Map<String,Object> map = new HashMap<>();
                    map.put("date",arrayList.get(i).get("date"));
                    map.put("time",arrayList.get(i).get("time"));
                    maps.add(map);
                }
            }
        }
        model.addObject("mapList",maps);
        model.setViewName("pay/pay_reportMonthRecycle");
        return model;
    }

    /*
     * 通过日期查询多条数据
     * @return 影响行数
     */
//    @GetMapping("DateselectPyUserHistory")
//    public List<Map<String,Object>> DateselectPyUserHistory(){
//
//    }
}